<template>
    <div class="app-container">
        <el-card class="box-card">
            <div class="flex justify-between">
                <el-button type="primary" size="default" @click="handleAdd">创建作品集</el-button>
            </div>
            <el-table
                ref="multipleTable"
                v-loading="loading"
                :data="tableData"
                border
                class="mt-3"
                stripe
                style="width: 100%"
                tooltip-effect="dark"
            >
                <el-table-column label="序号" type="index" :index="indexMethod" prop="user_code" width="100px"></el-table-column>
                <el-table-column label="作品集ID" prop="id"></el-table-column>
                <el-table-column label="作品集名称" prop="title"></el-table-column>
                <el-table-column label="作品集封面" prop="picture">
                    <template slot-scope="{row}">
                        <el-image v-if="row.picture" :src="row.picture" fit="fill" :lazy="true"></el-image>
                    </template>
                </el-table-column>
                <el-table-column label="粉丝数量" prop="fan_count"></el-table-column>
                <el-table-column label="作品数量" prop="count"></el-table-column>
                <el-table-column label="主播编号" prop="user.user_code"></el-table-column>
                <el-table-column label="主播昵称" prop="user.nickname"></el-table-column>
                <el-table-column label="排序" prop="sort"></el-table-column>
                <el-table-column label="上架状态" prop="status">
                    <template slot-scope="{row}">
                        <el-tag v-if="row.status === 0" type="danger">下架</el-tag>
                        <el-tag v-if="row.status === 1" type="success">上架</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="创建时间" prop="created_at"></el-table-column>
                
                <el-table-column label="操作" width="250px">
                    <template slot-scope="scope">
                        <el-button
                            size="mini"
                            type="text"
                            @click="handleCheckInfo(scope.row)"
                        >查看
                        </el-button>
                        <el-button
                            size="mini"
                            type="text"
                            @click="handleCheck(scope.row)"
                        >作品详情
                        </el-button>
                        <el-button
                            size="mini"
                            type="text"
                            @click="handleEdit(scope.row)"
                        >编辑
                        </el-button>
                        <el-button
                            v-if="scope.row.status === 0"
                            size="mini"
                            type="text"
                            @click="handleStatus(scope.row,1)"
                        >上架
                        </el-button>
                        <el-button
                            v-if="scope.row.status === 1"
                            size="mini"
                            type="text"
                            @click="handleStatus(scope.row,2)"
                        >下架
                        </el-button>
                        <el-button
                            size="mini"
                            type="text"
                            @click="handleDel(scope.row)"
                        >删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
    
            <!-- 分页 -->
            <cus-pagination
                v-show="total > 0"
                :limit.sync="listQuery.perPage"
                :page.sync="listQuery.page"
                :total="total"
                @pagination="getTableData"
            />
        </el-card>
  
        <el-dialog
            :title="dialogTitle"
            :visible.sync="dialogVisible"
            width="30%"
            @close="closeDialog('form')">
            
            <el-form ref="form" :inline="false" :model="formData" :rules="rules" label-width="170px" size="normal">
                <el-form-item label="作品集名称" prop="title">
                    <el-input
                    type="textarea"
                    rows="3"
                    v-model="formData.title"
                    placeholder="请输入作品集名称"
                    maxlength="50"
                    show-word-limit
                    />
                </el-form-item>
                
                <el-form-item label="作品集封面" prop="picture">
                    <div class="awardBox">
                        <div class="awardInfo" :class="{'hide_box': formData.picture, 'show_box': !formData.picture}">
                            <img v-if="formData.picture" :src="formData.picture" class="avatar" />
                            <el-upload
                                v-else
                                ref="upload"
                                :auto-upload="true"
                                :http-request="customizeUpload"
                                :on-success="handlePictureSuccess1"
                                action=""
                                class="upload-demo"
                                :limit="1"
                                list-type="picture-card"
                            >
                                <i slot="default" class="el-icon-plus"></i>
                            </el-upload>
                            <span class="closeIcon" @click="handlePictureRemove1"><i class="el-icon-delete"></i></span>
                        </div>
                    </div>
                </el-form-item>
                
                <el-form-item label="作品集简介" prop="introduction">
                    <el-input
                        type="textarea"
                        v-model="formData.introduction"
                        rows="8"
                        placeholder="请输入作品集简介"
                        size="normal"
                        maxlength="500"
                        show-word-limit
                    ></el-input>
                    
                </el-form-item>
                <el-form-item label="作品集作者" prop="user_code">
                    <el-input
                    v-model="formData.user_code"
                    placeholder="请输入作品集作者编号"
                    />
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input
                    v-model="formData.sort"
                    placeholder="请输入排序"
                    />
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-radio-group v-model="formData.status">
                        <el-radio :label="1">上架</el-radio>
                        <el-radio :label="0">下架</el-radio>
                    </el-radio-group>
                </el-form-item>
                
            </el-form>
            
            <span slot="footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="onSubmit">确定</el-button>
            </span>
        </el-dialog>

        <el-dialog
            title="作品集信息"
            :visible.sync="dialogVisible2"
            width="30%"
            @close="closeDialog('form')">
            
            <el-form ref="form" :inline="false" :model="formData" :rules="rules" label-width="170px" size="normal">
                <el-form-item label="作品集名称" prop="title">
                    <el-input
                    type="textarea"
                    rows="2"
                    v-model="formData.title"
                    placeholder="请输入作品集名称"
                    disabled
                    maxlength="50"
                    show-word-limit
                    />
                </el-form-item>
                
                <el-form-item label="作品集封面" prop="picture">
                    <div class="awardBox">
                        <div class="awardInfo" :class="{'hide_box': formData.picture, 'show_box': !formData.picture}">
                            <img v-if="formData.picture" :src="formData.picture" class="avatar" />
                        </div>
                    </div>
                </el-form-item>
                
                <el-form-item label="作品集简介" prop="introduction">
                    <el-input
                        type="textarea"
                        v-model="formData.introduction"
                        rows="5"
                        placeholder="请输入作品集简介"
                        size="normal"
                        disabled
                    ></el-input>
                    
                </el-form-item>
                <el-form-item label="作品集作者" prop="user_code">
                    <el-input
                    v-model="formData.user_code"
                    placeholder="请输入作品集作者编号"
                    disabled
                    />
                </el-form-item>
            </el-form>
            
            <span slot="footer">
                <el-button type="primary" @click="dialogVisible2 = false">确定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
  
<script>
    import { voiceList, voiceSave, voiceOff, voiceDelete } from '@/api/worksManagement'
  	export default {
        name: 'voicePortfolio',
        data() {
            return {
                dialogVisible: false,
                dialogVisible2: false,
                loading: false,
                tableData: [],
                listQuery: {
                    page: 1,
                    perPage: 10
                },
                total: 1,
                dialogTitle: '创建作品集',
                formData: {
                    id: undefined,
                    title: undefined,
                    picture: undefined,
                    sort: undefined,
                    user_code: undefined,
                    status: 1,
                    introduction: undefined,
                },
                rules: {
                    title: [{ required: true, message: '请输入名称', trigger: 'blur' }],
                    picture: [{ required: true, message: '请上传封面', trigger: 'blur' }],
                    user_code: [{ required: true, message: '请输入作者编号', trigger: 'blur' }],
                },
            }
        },
        created() {
            this.getTableData()
        },
        methods: {
            indexMethod(index) {
                return this.total - (this.listQuery.page-1) * this.listQuery.perPage - index
            },
            // 网络请求
            /**
             * getTableData
             * @desc 获取表格数据
             */
            async getTableData() {
                this.loading = true
                for (let key in this.listQuery) {
                    this.listQuery[key] = this.listQuery[key] === '' ? undefined : this.listQuery[key] == 0 ? 0 : this.listQuery[key]
                }
                const {data, code} = await voiceList(this.listQuery)
                if (code === 200) {
                    
                    data.list.map(item => {
                        item.user_code = item.user.user_code
                    })
                    this.tableData = data.list
                    this.total = data.meta.total
                }
                this.loading = false
        
            },
            closeDialog(form) {
                if (this.$refs[form]) {
                    this.$refs[form].clearValidate() // 清除整个表单的校验
                    this.$refs[form].resetFields() // 清除整个表单的校验
                }
                this.formData = {
                    id: undefined,
                    title: undefined,
                    picture: undefined,
                    sort: undefined,
                    user_code: undefined,
                    status: 1,
                    introduction: undefined,
                }
            },
            handleCheck(row) {
                this.$router.push({
                    path: 'worksList',
                    query: {
                        id: row.id
                    }
                })
            },
            handleCheckInfo(row) {
                this.dialogVisible2 = true
                this.formData = JSON.parse(JSON.stringify(row))
                // this.formData.user_code = this.formData.user.user_code
            },
            handleEdit(row) {
                this.dialogTitle = '编辑作品集'
                this.dialogVisible = true
                this.formData = JSON.parse(JSON.stringify(row))
                // this.formData.user_code = this.formData.user.user_code
            },
            handleStatus(row, type) {
                if(type == 1) {
                    this.$confirm('确定上架该作品集？', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        voiceOff({
                            id: row.id,
                            status: 1
                        }).then(res => {
                            if(res.code === 200) {
                                this.$message({
                                    type: 'success',
                                    message: '操作成功'
                                })
                            }
                            this.dialogVisible = false
                            this.getTableData()
                        })
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消'
                        });
                    });
                }
                if(type == 2) {
                    this.$confirm('确定下架该作品集？', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        voiceOff({
                            id: row.id,
                            status: 0
                        }).then(res => {
                            if(res.code === 200) {
                                this.$message({
                                    type: 'success',
                                    message: '操作成功'
                                })
                            }
                            this.dialogVisible = false
                            this.getTableData()
                        })
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消'
                        });
                    });
                }
                
            },
            handleAdd() {
                this.dialogTitle = '创建作品集'
                this.dialogVisible = true
            },
            handlePictureSuccess1() {
                this.formData.picture = this.ossLastImgUrl
            },
            handlePictureRemove1() {
                this.formData.picture = undefined
            },
            onSubmit() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        // for (let key in this.formData) {
                        //     this.formData[key] = this.formData[key] == '' ? undefined : this.formData[key] == 0 ? 0 : this.formData[key]
                        // }
                        voiceSave(this.formData).then(res => {
                            if(res.code === 200) {
                                this.$message({
                                    type: 'success',
                                    message: '操作成功'
                                })
                                this.dialogVisible = false
                                this.getTableData()
                            }
                        })
                    }
                })
            },
            handleDel(row) {
                console.log(row)
                this.$confirm('确定删除？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    voiceDelete({
                        id: row.id
                    }).then(res => {
                        if(res.code === 200) {
                            this.dialogVisible = false
                            this.$message({
                                type: 'success',
                                message: '操作成功'
                            })
                        }
                        this.getTableData()
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消操作'
                    });
                });
            },
        }
    }
</script>
  
<style lang="scss" scoped>
::v-deep{ 
    .hide_box{
        .el-upload--picture-card {
            display: none;
        }
    }
    .el-upload-list__item {
        margin-right: 1em !important;
    }
}
.radioStyle {
    width: 100px;
    margin-bottom: 15px;
}
.awardBox {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    width: 100%;
    .awardInfo {
        margin-right: 10px;
        border-radius: 4px;
        position: relative;
        overflow: hidden;
        width: 148px;
        height: 148px;
        border: 1px solid #c0ccda;
        border-radius: 6px;
        display: flex;
        align-items: center;
        justify-content: center;

        .closeIcon {
            position: absolute;
            top: 0;
            bottom: 0;
            right: 0;
            left: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 10px;
            background: rgba(0,0,0,.6);
            color: #ffffff;
            overflow: hidden;
            cursor: pointer;
            font-size: 18px;
            opacity: 0;
            z-index: 0;
        }
    }
    .hide_box:hover {
        .closeIcon {
            opacity: 1;
            z-index: 1;
        }
    }
    .show_box {
        .closeIcon {
            opacity: 0;
            z-index: -1;
        }
    }
}
.item {
    width: 100px;
}
.formItemBox{
	display: flex;
	align-items: center;
	flex-wrap: wrap;

	width: 100%;
	overflow: hidden;
}
::v-deep {
	.formItemBox{
		.el-form-item {
			width: 50% !important;
		}
	}
    
}

</style>
  